<?php
class Model_Product extends Zend_Db_Table_Abstract{
	protected $_name = "product";
	protected $_dependentTables = array("Model_BillProduct");
	protected $_referenceMap = array(
		"Manufacturer" => array(
			"columns" 		=> "man_id",
			"refTableClass" => "Model_Manufacturer",
			"refColumns"	=> "id"
		),
		"Category" => array(
			"columns"		=> "category_id",
			"refTableClass" => "Model_PCategory",
			"refColumns"	=> "id"
		)
	);
	public function getAllProducts($man_id = 0, $cat_id =0 , $model = ''){
		try{
		$select = $this->select();
		
		if($man_id != 0 ) $select = $select->where("man_id=". $man_id);
		if($cat_id != 0 ) $select = $select->where("category_id=". $cat_id);
		if($model != '' ) $select->where("name like '%".$model."%'")
						->orWhere("MATCH (name) AGAINST ('".$model."' IN BOOLEAN MODE)")
						->orWhere("model LIKE '%".$model."%'")
						->orWhere("MATCH (model) AGAINST ('".$model."' IN BOOLEAN MODE)");
		return $this->fetchAll($select);
		}catch(Exception $ex){
			echo $ex->getMessage();
			return null;
		}
	}
	/**
	 * Đếm số lượng sản phẩm trong bảng
	 * @return int total-product
	 */
	function countProduct(){
		 $select = $this->_db->select()->from('product',array("COUNT(product.id) as total"));
		 return $this->_db->fetchOne($select);
		
	}
	/**
	 * Đếm số lượng sản phẩm theo pcategory_id
	 * @return int $totalProductInPcategory_id
	 */
	function countProductInPCategoryID($pcategory_id){
		 $select = $this->_db->select()->from('product',array("COUNT(product.id) as total"))->where('category_id ='.$pcategory_id);
		 return $this->_db->fetchOne($select);
		
	}
	public function getProductByID($id){
		return $this->find($id)->current();
	}
	public function getProductBySlug($slug){
		return $this->fetchRow("slug ='".$slug."'");
		
	}
	public function getProductsByKeyword($keyword, $psort= ''){
		$select = $this->select()
						->where("name like '%".$keyword."%'")
						->orWhere("MATCH (name) AGAINST ('".$keyword."' IN BOOLEAN MODE)")
						->orWhere("model LIKE '%".$keyword."%'")
						->orWhere("MATCH (model) AGAINST ('".$keyword."' IN BOOLEAN MODE)");
		if($psort != '') $select->order('price '.$psort);
		return $this->fetchAll($select);
	}
	/**
	 * 
	 * Lấy các sản phẩm liên quan
	 * @param Zend_Db_Table_Row $product Đối tượng user
	 * @param int $num Số bản ghi muốn lấy
	 */
	public function getRelateProducts($product, $num = 3){
		return $this->fetchAll($this->select()->where('category_id ='.$product->category_id )->where('id <>'.$product->id)->order('created_date desc')->limit($num));
	}
	public function getProductNumberByFilter($man_id, $cat_id, $arrFilterValueIDs){
		$strFilterValueIDs ='';
		
		if(is_array($arrFilterValueIDs) && isset($arrFilterValueIDs) && count($arrFilterValueIDs)){
			$strFilterValueIDs = implode(',', $arrFilterValueIDs);
		}
		$select = $this->_db->select()
						->from('product', array('id', 'name', 'image','model', 'price', 'slug', 'description'))
						->joinLeft('product_filtervalue', 'product.id = product_filtervalue.product_id', array())
						->joinLeft('product_category', 'product.category_id = product_category.id', array('parent_id'));
		if(isset($man_id) && $man_id != '') $select = $select->where('man_id = '. $man_id);
		if(isset($cat_id) && $cat_id != '') $select = $select->where('category_id = '. $cat_id.' OR '.'product_category.parent_id =' . $cat_id);
		if($strFilterValueIDs != '') $select = $select->where('filtervalue_id in ('.$strFilterValueIDs.')');
		$select->group('product.id');
		$stmt = $this->_db->query($select);
		$result = $stmt->fetchAll();
		
		return count($result);								
						
	}
	public function filterProduct($man_id, $cat_id, $arrFilterValueIDs, $priceOrder =''){
		$strFilterValueIDs ='';
		if(is_array($arrFilterValueIDs) && isset($arrFilterValueIDs) && count($arrFilterValueIDs)){
			$strFilterValueIDs = implode(',', $arrFilterValueIDs);
		}
		$select = $this->_db->select()
						->from('product', array('id', 'name', 'image','model', 'price', 'slug', 'description'))
						->joinLeft('product_filtervalue', 'product.id = product_filtervalue.product_id', array())
						->joinLeft('product_category', 'product.category_id = product_category.id', array('parent_id'));
		if(isset($man_id) && $man_id != '') $select = $select->where('man_id = '. $man_id);
		if(isset($cat_id) && $cat_id != '') $select = $select->where('category_id = '. $cat_id.' OR '.'product_category.parent_id =' . $cat_id);
		if($strFilterValueIDs != '') $select = $select->where('filtervalue_id in ('.$strFilterValueIDs.')');
		if($priceOrder !='' ) $select = $select->order("price ".$priceOrder);
		$select->group('product.id');	
		$stmt = $this->_db->query($select);
		return $result = $stmt->fetchAll();
	}
	public function createProduct($name, $model, $price,$show_on_home, $image, $specification, $ink, $components, $description, $short_description, $page_title, $page_description, $created_date, $man_id, $slug, $category_id, $tag, $arrFilterValues){
		$product = $this->createRow();
		$product->name = $name;
		$product->model = $model;
		$product->price = $price;
		$product->show_on_home = $show_on_home;
		$product->image = $image;
		$product->specification = $specification;
		$product->ink = $ink;
		$product->components = $components;
		$product->description = $description;
		$product->short_description = $short_description;
		$product->page_title = $page_title;
		$product->page_description = $page_description;
		$product->created_date = $created_date;
		$product->man_id = $man_id;
		$product->slug = $slug;
		$product->category_id = $category_id;
		$product->tag = $tag;
		$product->save();
		try{
			$product_id = $this->_db->lastInsertId();
			$productFilterValueModel = new Model_ProductFilterValue();
			foreach ($arrFilterValues as $filterValue) {
				$productFilterValueModel->createProductFilterValue($product_id, $filterValue);
			}
			return true;
		}catch(Exception $ex){
			echo "Caught exception: " + $ex->getMessage();
			return false;
		}

	}
	public function updateProduct($id, $name, $model, $price,$show_on_home, $image, $specification, $ink, $components, $description, $short_description, $page_title, $page_description, $created_date, $man_id, $slug, $category_id, $tag,$arrFilterValues){
		$data = array(
			'name' => $name,
			'model' => $model,
			'price' => $price,
			'show_on_home'=>$show_on_home,
			'image' => $image,
			'specification' => $specification,
			'ink' => $ink,
			'components' => $components,
			'description' => $description,
			'short_description' => $short_description,
			'page_title' => $page_title,
			'page_description' => $page_description,
			'created_date' => $created_date,
			'man_id' => $man_id,
			'slug' => $slug,
			'category_id' => $category_id,
			'tag' => $tag
		
		);
		try{
			$this->update($data, 'id = '.$id);
			$productFilterValueModel = new Model_ProductFilterValue();
			$productFilterValueModel->updateProductFilterValue($id, $arrFilterValues);
			return true;
		}catch(Exception $ex){
			echo "Caught exception: " + $ex->getMessage();
			return false;
		}
	}
	public function deleteProduct($id){
		if(is_numeric($id)){
			return $this->delete('id =' .$id);
		}else{
			return $this->delete('id in ('.$id.')');
		}
	}
}